package com.ruoyi.web.controller.ems;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.HUAWEI.EmsBarcodeMaterial;
import com.ruoyi.system.domain.HUAWEI.EmsBarcodeRelation;
import com.ruoyi.system.service.IEmsBarcodeMaterialService;
import com.ruoyi.system.service.IEmsBarcodeRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/4/10 15:31
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@Api(tags = "批次物料追溯api")
@RequestMapping("/ems/barcodeMaterial")
public class EmsBarcodeMaterialController extends BaseController {

    @Autowired
    IEmsBarcodeMaterialService iEmsBarcodeMaterialService;


    /**
     * 查询批次物料追溯
     */
    @ApiOperation(value = "查询-批次物料追溯")
    @GetMapping("/list")
    public TableDataInfo list(EmsBarcodeMaterial emsBarcodeMaterial) {
        startPage();
        LambdaQueryWrapper<EmsBarcodeMaterial> lqw = new LambdaQueryWrapper<EmsBarcodeMaterial>();
        //工单号
        if (StringUtils.isNotBlank(emsBarcodeMaterial.getPo())) {
            lqw.like(EmsBarcodeMaterial::getPo, emsBarcodeMaterial.getPo());
        }
        //产品编码
        if (StringUtils.isNotBlank(emsBarcodeMaterial.getItemCode())) {
            lqw.like(EmsBarcodeMaterial::getItemCode, emsBarcodeMaterial.getItemCode());
        }
        //产品条码
        if (StringUtils.isNotBlank(emsBarcodeMaterial.getBarCode())) {
            lqw.like(EmsBarcodeMaterial::getBarCode, emsBarcodeMaterial.getBarCode());
        }
        //上传标识
        if (emsBarcodeMaterial.getUploadFlag() != null) {
            lqw.eq(EmsBarcodeMaterial::getUploadFlag, emsBarcodeMaterial.getUploadFlag());
        }
        //采集时间
        if (null != emsBarcodeMaterial.getBeginTime()) {
            lqw.apply("created_date>='" + DateTime.of(emsBarcodeMaterial.getBeginTime()).toString(DatePattern.NORM_DATETIME_MS_PATTERN) + "'");
        }
        if (null != emsBarcodeMaterial.getEndTime()) {
            lqw.apply("created_date<='" + DateTime.of(emsBarcodeMaterial.getEndTime()).toString(DatePattern.NORM_DATETIME_MS_PATTERN) + "'");
        }
        //物料09码
        if (StringUtils.isNotBlank(emsBarcodeMaterial.getSegment1())) {
            lqw.like(EmsBarcodeMaterial::getSegment1, emsBarcodeMaterial.getSegment1());
        }
        //华为任务令
        if (StringUtils.isNotBlank(emsBarcodeMaterial.getSegment4())) {
            lqw.like(EmsBarcodeMaterial::getSegment4, emsBarcodeMaterial.getSegment4());
        }
        //产品编码
        if (StringUtils.isNotBlank(emsBarcodeMaterial.getBarItemCode())) {
            lqw.like(EmsBarcodeMaterial::getBarItemCode, emsBarcodeMaterial.getBarItemCode());
        }
        //物料PSN码
        if (StringUtils.isNotBlank(emsBarcodeMaterial.getSegment13())) {
            lqw.like(EmsBarcodeMaterial::getSegment13, emsBarcodeMaterial.getSegment13());
        }

        List<EmsBarcodeMaterial> list = iEmsBarcodeMaterialService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 新增批次物料追溯
     */
    @Log(title = "新增批次物料追溯", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增-批次物料追溯")
    @PostMapping
    public AjaxResult add(@RequestBody EmsBarcodeMaterial emsBarcodeMaterial) {
        emsBarcodeMaterial.setCreatedBy(SecurityUtils.getUsername());
        emsBarcodeMaterial.setCreatedDate(new Date());
        return toAjax(iEmsBarcodeMaterialService.save(emsBarcodeMaterial) ? 1 : 0);
    }

    /**
     * 修改批次物料追溯
     */
    @Log(title = "修改批次物料追溯", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改-批次物料追溯")
    @PutMapping
    public AjaxResult edit(@RequestBody EmsBarcodeMaterial emsBarcodeMaterial) {
        emsBarcodeMaterial.setLastUpdatedBy(SecurityUtils.getUsername());
        emsBarcodeMaterial.setLastUpdatedDate(new Date());
        return toAjax(iEmsBarcodeMaterialService.updateById(emsBarcodeMaterial) ? 1 : 0);
    }

    /**
     * 删除批次物料追溯
     */
    @Log(title = "删除批次物料追溯", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除-批次物料追溯")
    @DeleteMapping("/{teamIds}")
    public AjaxResult remove(@PathVariable Long[] teamIds) {
        return toAjax(iEmsBarcodeMaterialService.removeByIds(Arrays.asList(teamIds)) ? 1 : 0);
    }

}

